package com.example.training.config;

import com.example.training.model.errorcode.BaseErrorCode;
import com.example.training.model.exception.AbstractException;
import com.example.training.model.exception.ClientException;
import com.example.training.model.web.Result;
import com.example.training.model.web.Results;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import javax.servlet.http.HttpServletRequest;
import java.util.Optional;


@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {


    @SneakyThrows
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public Result validExceptionHandler(HttpServletRequest request, MethodArgumentNotValidException ex) {
        BindingResult bindingResult = ex.getBindingResult();
        FieldError firstFieldError= null;
        if (bindingResult.hasErrors()){
            firstFieldError = bindingResult.getFieldErrors().get(0);
        }
        String exceptionStr = Optional.ofNullable(firstFieldError)
                .map(FieldError::getDefaultMessage)
                .orElse("");
        return Results.failure(BaseErrorCode.CLIENT_ERROR.code(), exceptionStr);
    }

    @ExceptionHandler(value = {AbstractException.class})
    public Result abstractException(HttpServletRequest request, AbstractException ex) {
        if (ex.getCause() != null) {
            return Results.failure(ex);
        }
        return Results.failure(ex);
    }

    @ExceptionHandler(value = Throwable.class)
    public Result defaultErrorHandler(HttpServletRequest request, Throwable throwable) {
        System.out.println(throwable);
        return Results.failure();
    }

    @ExceptionHandler(value = {MethodArgumentTypeMismatchException.class})
    public Result defaultErrorHandler(HttpServletRequest request, MethodArgumentTypeMismatchException throwable) {
        return Results.failure(new ClientException(BaseErrorCode.PARAM_ERROR));
    }
    @ExceptionHandler(value = {HttpMessageNotReadableException.class})
    public Result defaultErrorHandler(HttpServletRequest request, HttpMessageNotReadableException throwable) {
        return Results.failure(new ClientException(BaseErrorCode.PARAMUNOTREAD_ERROR));
    }

    private String getUrl(HttpServletRequest request) {
        if (StringUtils.isEmpty(request.getQueryString())) {
            return request.getRequestURL().toString();
        }
        return request.getRequestURL().toString() + "?" + request.getQueryString();
    }
}